import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/store.js'
import de from "element-ui/src/locale/lang/de";

const Login = () => import('views/login/Login')
const  Index = () => import('../views/Index')
    const office = () => import('../views/office/OfficeList')
    const User = () => import('../views/user/UserList')
    const Role = () => import('../views/role/RoleList')
    const Menu = () => import('../views/menu/MenuList')
    const Param = () => import('../views/param/ParamList')
    const Log = () => import('../views/log/LogList')
    const Swiper = () => import('../views/swiper/SwiperList')
    const Colunm = () => import('../views/colunm/ColunmList')
    const Service = () => import('../views/service/ServiceList')
    const Honor = () => import('../views/honor/HonorList')
    const Case = () => import('../views/case/CaseList')
    const Culture = () => import('../views/culture/CultureList')
    const Notice = () => import('../views/notice/NoticeList')
    const News = () => import('../views/news/NewsList')
    const Job = () => import('../views/job/JobList')
    const Message = () => import('../views/message/MessageList')
    const ProductType = () => import('../views/productType/ProductTypeList')
    const Product = () => import('../views/product/ProductList')
    const Link = () => import('../views/link/LinkList')
    const about = () => import('../views/about/AboutList')
    const MenuRole = () => import('../views/menuRole/MenuRoleForm')

Vue.use(Router)

const routes = [
    {
        path: '/',
        redirect: '/index'
    },
   {
        path: '/login',
        name: "登陆",
        component: Login
    },{
        path: '/index',
        name: "首页",
        component: Index,
        children: [
            {
                path: '/office',
                name: "公司信息",
                component: office,
            },
            {
                path: '/user',
                name: "用户管理",
                component: User,
            },
            {
                path: '/role',
                name: "角色管理",
                component: Role,
            },
            {
                path: '/menuRole',
                name: "权限分配",
                component: MenuRole,
            },
            {
                path: '/menu',
                name: "菜单管理",
                component: Menu,
            },
            {
                path: '/param',
                name: "参数设置",
                component: Param,
            },
            {
                path: '/log',
                name: "日志管理",
                component: Log,
            },
            {
                path: '/swiper',
                name: "轮播图",
                component: Swiper,
            },
            {
                path: '/colunm',
                name: "栏目管理",
                component: Colunm,
            },
            {
                path: '/service',
                name: "服务管理",
                component: Service,
            },
            {
                path: '/honor',
                name: "荣誉资质",
                component: Honor,
            },
            {
                path: '/case',
                name: "客户案例",
                component: Case,
            },
            {
                path: '/culture',
                name: "企业风采",
                component: Culture,
            },
            {
                path: '/notice',
                name: "公告管理",
                component: Notice,
            },
            {
                path: '/news',
                name: "新闻管理",
                component: News,
            },
            {
                path: '/job',
                name: "招聘管理",
                component: Job,
            },
            {
                path: '/message',
                name: "留言管理",
                component: Message,
            },
            {
                path: '/productType',
                name: "产品分类",
                component: ProductType,
            },
            {
                path: '/product',
                name: "产品管理",
                component: Product,
            },
            {
                path: '/link',
                name: "友情链接",
                component: Link,
            },
            {
                path: '/about',
                name: "关于我们",
                component: about,
            },
        ]
    }
]


const router = new Router({
    routes,
    mode: 'history'
})

// 解决重复点击导航路由报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


router.beforeEach((to,from,next)=>{
    let token = localStorage.getItem('cmsToken');
    if (to.path == '/login'){
        if (token!='' && token != null){
            router.push("/index")
        }
        next()
    }else {
        if (token!='' && token != null){
            (async function () {
                await doNext(to, next)
            })();
        }else{
            router.push("/login")
        }
    }
})


async function doNext(to, next) {

    let doRefresh = store.getters.getIsFirst

    if (doRefresh) {
        store.dispatch('SetIsFirst')
        await store.dispatch("SetCmsUser");
        await store.dispatch("SetCmsMenu");
        store.dispatch("SetCmsRole");
        //store.dispatch("SetCmsRouter");
        next({...to, replace: true});
    }
    next();
}

router.afterEach((to, from) => {
    if (to.path!='/login'){
        store.commit('addOpenList', to)
    }
})

export default router
